﻿using Combat;
using UnityEngine;

namespace Visual
{
    public class WeaponFirePulser : MonoBehaviour
    {
        [SerializeField] private float pulseScale = 1.15f;
        [SerializeField] private float recoverSpeed = 5f;

        private Vector3 _defaultScale = Vector3.one;


        private void Awake()
        {
            _defaultScale = transform.localScale.x * Vector3.one;
        }

        private void OnEnable()
        {
            Weapon.AnyWeaponFired += AnyWeaponFired;
        }

        private void OnDisable()
        {
            Weapon.AnyWeaponFired -= AnyWeaponFired;
        }

        private void Update()
        {
            transform.localScale = Vector3.Lerp(
                transform.localScale, _defaultScale, 
                recoverSpeed * Time.deltaTime);
        }
        
        private void AnyWeaponFired(Team team, Weapon _)
        {
            if (team == Team.Player)
            {
                Pulse();
            }
        }

        private void Pulse()
        {
            transform.localScale = pulseScale * Vector3.one;
        }
    }
}